home *** CD-ROM | disk | FTP | other *** search
- /*****************************************************************************
- * FILE: sndtest.c
- * AUTHOR: David Hay
- * CREATED: March 9, 1995
- * DESCRIPTION: Test program and tutorial for using the Caveman Sound System.
- *
- * Copyright © 1995-1997 David Hay
- *
- * Permission to use, copy, and distribute this software and its documentation
- * for any purpose is hereby granted without fee, provided that (i) the above
- * copyright notices and this permission notice appear in all copies of the
- * software and related documentation, and (ii) the names of David Hay and
- * Caveman Creations may not be used in any advertising or publicity relating
- * to the software without the specific, prior written permission of David Hay
- *
- * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS,
- * IMPLIED OR OTHERWISE, INCLUDING, WITHOUT LIMITATION, ANY WARRANTY OF
- * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
- * DAVID HAY OR CAVEMAN CREATIONS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
- * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
- * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
- * POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
- * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- *****************************************************************************/
-
- #ifndef __CMSOUNDSYSTEM__
- #include "CMSoundSystem.h"
- #endif
-
-
- #define kNumChannels 1 /* The number of sound channels to open */
- #define kSoundToPlay 31000 /* Resource ID of the sound to play */
- #define kSoundToPlay2 128 /* Resource ID of another sound to play */
-
-
- /* Standard Mac Toolbox initialization */
- static void
- InitMacintosh( void )
- {
- MaxApplZone();
-
- InitGraf( &qd.thePort );
- InitFonts();
- FlushEvents( everyEvent, 0 );
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( 0L );
- InitCursor();
- }
-
- /*---------------------------------------------------------------------------*/
-
- void main( void )
- {
- short ii; /* misc counter */
- short sndRef; /* sound reference number for sound 1 */
- short sndRef2; /* sound reference number for sound 2 */
- long ignored;
- OSErr err;
-
- InitMacintosh();
-
- /* First you must initialize the sound system by specifying the
- ** number of channels you wish to use.
- **
- ** • 1.0 - Initialization of the sound tool has been simplified since
- ** version 1.0. Previously one had to call CMSInitSound() and
- ** CMSOpenAllChannels().
- */
- err = CMSOpenSoundTool( kNumChannels );
-
-
- /* In order to play a sound, we first need to load it in. You give
- ** a resource number and it loads that sound resource and registeres
- ** it with the sound system. A reference number is returned in the
- ** second parameter.
- */
- if ( err == noErr )
- err = CMSLoadSound( kSoundToPlay, &sndRef );
-
- if ( err == noErr )
- err = CMSLoadSound( kSoundToPlay2, &sndRef2 );
-
-
- /* Let's see what happens when we remove a sound and the load it
- ** back in. Normally you wouldn't do this right after you loaded
- ** it, but this is as good a place as any to show how it's done.
- */
- if ( err == noErr )
- err = CMSRemoveSound( sndRef );
-
- if ( err == noErr )
- err = CMSLoadSound( kSoundToPlay, &sndRef );
-
-
- /* Now that we've loaded a sound, play it using the reference number
- ** we got from CMSLoadSound() or CMSRegisterSound(), depending on
- ** how the sound was registered. We'll use sound channel 0.
- */
- if ( err == noErr )
- err = CMSPlaySound( sndRef, 0 );
-
- /* Let's play with the volume. Wait 2 seconds, then drop the
- ** sound to 50%, wait another 2 seconds then back to full volume.
- **
- ** • 1.1 - CMSSetChannelVolume() is a new function.
- */
- if ( err == noErr )
- {
- Delay( 2L * 60L, &ignored );
- err = CMSSetChannelVolume( 0, 0 );
- }
-
- if ( err == noErr )
- {
- Delay( 2L * 60L, &ignored );
- err = CMSSetChannelVolume( 0, 256 );
- }
-
- /* Since we played the last sound asynchronously, we want to
- ** find out when it stops so we can do other stuff. To do
- ** this we use the CMSGetSoundPlaying() routine. This returns
- ** the sound reference number of the currently playing sound,
- ** kSoundResource or kNoSound in the sndRef paramater. I
- ** could have used the routine CMSWaitForSilence() here, but
- ** very often the sound was played asynchronously so you could
- ** do other stuff while the sound is playing. Plus I wanted to
- ** show how the CMSGetSoundPlaying() function worked. ;-)
- */
- while ( err == noErr )
- {
- err = CMSGetSoundPlaying( 0, &sndRef );
- if ( sndRef == kNoSound )
- break;
- }
-
-
- /* Now let's play some music. First call CMSLoadMusic() to
- ** load the music blocks into memory and register them with
- ** the sound system.
- */
- if ( err == noErr )
- err = CMSLoadMusic( 128 );
-
- if ( err == noErr )
- {
- /* Start the music going with CMSPlayMusic(). Here we indicate
- ** that the music should be played on sound channel 0. This
- ** will play the music asynchronously. If a loop has been
- ** defined in the 'MUSL' resource, then the music will loop
- ** until it is stopped with a call to CMSStopSound() or another
- ** sound is played on the same channel. My sound sample does
- ** have a loop, so it will just keep going until we interrupt it
- */
- err = CMSPlayMusic( 0 );
- }
-
- /* Lets try out the sound priority mechanism and interrupt the music
- ** with another sound. By default, every sound has a priority of 0,
- ** but we can override it with a higher priority. Here we'll use 5.
- **
- ** • 1.1 - CMSPlaySoundPriority() is a new function.
- */
- if ( err == noErr )
- {
- Delay( 4L * 60L, &ignored );
- err = CMSPlaySoundPriority( sndRef2, 5 );
- }
-
- /* Wait for that last sound to finish playing. The check on err
- ** is a result of the return value from CMSPlaySoundPriority() which
- ** will return kPriorityOverride when a sound was overridden by the
- ** new sound. (i.e. we were able to start the sound)
- */
- if ( err == noErr || err == kPriorityOverride )
- err = CMSWaitForSilence( 0 );
-
-
- /* Now start the music again, wait for a while, then fade out */
-
- if ( err == noErr )
- err = CMSPlayMusic( 0 );
-
- if ( err == noErr )
- {
- Delay( 10L * 60L, &ignored );
-
- for ( ii = 256; ii >= 0 && err == noErr; ii -= 8 )
- {
- err = CMSSetChannelVolume( 0, ii );
- Delay( 10L, &ignored );
- }
- }
-
- /* We're finished with the sound system, so call CMSDisposeSound()
- ** to close and free the sound channels and any sounds registered
- ** with the sound system.
- */
- CMSDisposeSound();
-
- FlushEvents( everyEvent, 0 );
- if ( err != noErr ) SysBeep( 10 );
- }